package bobaini.com.fanout_exchange;

import bobaini.com.util.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * ClassName: Product
 * author: bob.ly
 * Version: 1.0.0
 * DateTime: 2021/07/25-20:29:00
 * Description: 消息接收
 */
public class Consumer1 {
    private static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);

        /**
         * 声明队列名称,临时队列可以直接调用API
         */
        String queueName = channel.queueDeclare().getQueue();

        /**
         * 交换机和队列进行绑定
         */
        channel.queueBind(queueName, EXCHANGE_NAME, "");
        System.out.println("等待接受信息,把接受的信息打印到屏幕上...");

        /**
         * 成功消费消息,则调用该方法,此处可以处理业务逻辑,message就是传递过来的消息,一般是对象
         */
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("接受消息:" + new String(message.getBody(), "UTF-8"));
        };

        /**
         * 失败调用回调函数
         */
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
        });
    }
}
